package com.yaoli.meituan;
import java.util.*;

public class Main{
    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        int sort = input.nextInt();

        //升序
        Comparator<Person> comp1 = new Comparator<Person>(){
            public int compare(Person o1,Person o2){
                return o1.grade - o2.grade;
            }
        };

        //降序
        Comparator<Person> comp2 = new Comparator<Person>(){
            public int compare(Person o1,Person o2){
                return o2.grade - o1.grade;
            }
        };


        ArrayList<Person> list = new ArrayList<Person>();

        for(int i = 0 ; i < num; i++){
            Person p = new Person();
            p.name = input.next();
            p.grade = input.nextInt();
            list.add(p);
        }

        if(sort == 0){
            Collections.sort(list,comp2);
        }else{
            Collections.sort(list,comp1);
        }

        for(int i = 0 ; i < num ; i++){
            System.out.println(list.get(i).name+" "+list.get(i).grade);
        }
    }
}


class Person{
    String name;
    int grade;
}